<?php

/*============================================
Classe Iacommand
============================================*/

class Iacommand extends ModelObject{

	protected static $cached_object_list= array();
	protected $COMNAME;
	protected $COMCOM;
	protected $DEVICE_NAME;
	protected $COM_OPE;
	protected $LOCKING_AUTHORIZATION;
	protected $LOCKING_STATUS;
	protected $COMVARS;
	protected $CMD_DEFAULT_VALUES;
	protected $COMSTRING;
	protected $VARCOM;
	protected $COMCOND;
	protected $COMEFF;
	protected $MAX_DURATION;

	protected function __construct( $COMNAME,$COMCOM,$DEVICE_NAME,$COM_OPE,$LOCKING_AUTHORIZATION,$LOCKING_STATUS,$COMVARS,$CMD_DEFAULT_VALUES,$COMSTRING,$VARCOM,$COMCOND,$COMEFF,$MAX_DURATION){
		try{
			
			$this->COMNAME = $COMNAME;
			$this->COMCOM = $COMCOM;
			$this->DEVICE_NAME = $DEVICE_NAME;
			$this->COM_OPE = $COM_OPE;
			$this->LOCKING_AUTHORIZATION = $LOCKING_AUTHORIZATION;
			$this->LOCKING_STATUS = $LOCKING_STATUS;
			$this->COMVARS = $COMVARS;
			$this->CMD_DEFAULT_VALUES = $CMD_DEFAULT_VALUES;
			$this->COMSTRING = $COMSTRING;
			$this->VARCOM = $VARCOM;
			$this->COMCOND = $COMCOND;
			$this->COMEFF = $COMEFF;
			$this->MAX_DURATION = $MAX_DURATION;
		}
		catch(Excpetion $e){
			throw new Exception("La construction de l'objet de type get_class($this) a échouée",0,$e);
		}
	}

	public function getCOMNAME(){
		return $this->COMNAME;
	}

	public function getCOMCOM(){
		return $this->COMCOM;
	}

	public function getDEVICENAME(){
		return DEVICENAME::get($this->DEVICE_NAME);
	}

	public function getCOMOPE(){
		return $this->COM_OPE;
	}

	public function getLOCKINGAUTHORIZATION(){
		return $this->LOCKING_AUTHORIZATION;
	}

	public function getLOCKINGSTATUS(){
		return $this->LOCKING_STATUS;
	}

	public function getCOMVARS(){
		return $this->COMVARS;
	}

	public function getCMDDEFAULTVALUES(){
		return $this->CMD_DEFAULT_VALUES;
	}

	public function getCOMSTRING(){
		return $this->COMSTRING;
	}

	public function getVARCOM(){
		return $this->VARCOM;
	}

	public function getCOMCOND(){
		return $this->COMCOND;
	}

	public function getCOMEFF(){
		return $this->COMEFF;
	}

	public function getMAXDURATION(){
		return $this->MAX_DURATION;
	}

	public function setCOMCOM( $COMCOM){
		try{
			$temp = $this->COMCOM;
			if($this->COMCOM !== $COMCOM){
				$this->COMCOM = $COMCOM;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->COMCOM = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setCOMCOM() a échouée",0,$e);
		}
	}

	public function setDEVICENAME( $DEVICE_NAME){
		try{
			$temp = $this->DEVICE_NAME;
			if(is_null($DEVICE_NAME)){
				throw new Exception("Le paramètre \$dEVICE_NAME de type get_class($dEVICE_NAME) est null alors que la colonne dans la base est marquée comme NOT NULL");
			}
			else if(!($DEVICE_NAME instanceof DEVICENAME) || is_null($DEVICE_NAME->getId())){
				throw new Exception("Le paramètre \$dEVICE_NAME de type get_class($dEVICE_NAME) n'est pas du bon type ou n'est pas présent dans la base");
			}
			else {
				if($this->DEVICE_NAME !== $DEVICE_NAME->getId()){
					$this->DEVICE_NAME = $DEVICE_NAME->getId();
					$this->update();
				}
			}
		}
		catch(Exception $e){
			$this->DEVICE_NAME = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setDEVICENAME() a échouée",0,$e);
		}
	}

	public function setCOMOPE( $COM_OPE){
		try{
			$temp = $this->COM_OPE;
			if($this->COM_OPE !== $COM_OPE){
				$this->COM_OPE = $COM_OPE;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->COM_OPE = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setCOMOPE() a échouée",0,$e);
		}
	}

	public function setLOCKINGAUTHORIZATION( $LOCKING_AUTHORIZATION){
		try{
			$temp = $this->LOCKING_AUTHORIZATION;
			if($this->LOCKING_AUTHORIZATION !== $LOCKING_AUTHORIZATION){
				$this->LOCKING_AUTHORIZATION = $LOCKING_AUTHORIZATION;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->LOCKING_AUTHORIZATION = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setLOCKINGAUTHORIZATION() a échouée",0,$e);
		}
	}

	public function setLOCKINGSTATUS( $LOCKING_STATUS){
		try{
			$temp = $this->LOCKING_STATUS;
			if($this->LOCKING_STATUS !== $LOCKING_STATUS){
				$this->LOCKING_STATUS = $LOCKING_STATUS;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->LOCKING_STATUS = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setLOCKINGSTATUS() a échouée",0,$e);
		}
	}

	public function setCOMVARS( $COMVARS){
		try{
			$temp = $this->COMVARS;
			if($this->COMVARS !== $COMVARS){
				$this->COMVARS = $COMVARS;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->COMVARS = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setCOMVARS() a échouée",0,$e);
		}
	}

	public function setCMDDEFAULTVALUES( $CMD_DEFAULT_VALUES){
		try{
			$temp = $this->CMD_DEFAULT_VALUES;
			if($this->CMD_DEFAULT_VALUES !== $CMD_DEFAULT_VALUES){
				$this->CMD_DEFAULT_VALUES = $CMD_DEFAULT_VALUES;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->CMD_DEFAULT_VALUES = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setCMDDEFAULTVALUES() a échouée",0,$e);
		}
	}

	public function setCOMSTRING( $COMSTRING){
		try{
			$temp = $this->COMSTRING;
			if($this->COMSTRING !== $COMSTRING){
				$this->COMSTRING = $COMSTRING;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->COMSTRING = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setCOMSTRING() a échouée",0,$e);
		}
	}

	public function setVARCOM( $VARCOM){
		try{
			$temp = $this->VARCOM;
			if($this->VARCOM !== $VARCOM){
				$this->VARCOM = $VARCOM;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->VARCOM = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setVARCOM() a échouée",0,$e);
		}
	}

	public function setCOMCOND( $COMCOND){
		try{
			$temp = $this->COMCOND;
			if($this->COMCOND !== $COMCOND){
				$this->COMCOND = $COMCOND;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->COMCOND = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setCOMCOND() a échouée",0,$e);
		}
	}

	public function setCOMEFF( $COMEFF){
		try{
			$temp = $this->COMEFF;
			if($this->COMEFF !== $COMEFF){
				$this->COMEFF = $COMEFF;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->COMEFF = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setCOMEFF() a échouée",0,$e);
		}
	}

	public function setMAXDURATION( $MAX_DURATION){
		try{
			$temp = $this->MAX_DURATION;
			if($this->MAX_DURATION !== $MAX_DURATION){
				$this->MAX_DURATION = $MAX_DURATION;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->MAX_DURATION = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setMAXDURATION() a échouée",0,$e);
		}
	}

	public static function create( $COMNAME,$COMCOM,$DEVICE_NAME,$COM_OPE,$LOCKING_AUTHORIZATION,$LOCKING_STATUS,$COMVARS,$CMD_DEFAULT_VALUES,$COMSTRING,$VARCOM,$COMCOND,$COMEFF,$MAX_DURATION){
		try{
			$iacommand = new Iacommand($COMNAME, $COMCOM, $DEVICE_NAME, $COM_OPE, $LOCKING_AUTHORIZATION, $LOCKING_STATUS, $COMVARS, $CMD_DEFAULT_VALUES, $COMSTRING, $VARCOM, $COMCOND, $COMEFF, $MAX_DURATION);
			$iacommand->insert();
			Iacommand::cacheObject($iacommand);
			return $iacommand;
		}
		catch(Excpetion $e){
			throw new Exception("La création de l'objet de type Iacommand a échouée",0,$e);
		}
	}

	public static function get( $COMNAME){
		$dbh = SingletonPDO::getInstance();
		try{
			if(is_null($COMNAME) || $COMNAME === "") {
				return null;
			}
			if(Iacommand::existsInCache($COMNAME)) {
				return Iacommand::getFromCache($COMNAME);
			}
			$dbh->beginTransaction();
			$sql= 'SELECT * FROM `iacommand` WHERE `COMNAME` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':id'=>$COMNAME));
			if ($row = $stmt->fetch()){
				$iacommand = new Iacommand( $row['COMNAME'],$row['COMCOM'],$row['DEVICE_NAME'],$row['COM_OPE'],$row['LOCKING_AUTHORIZATION'],$row['LOCKING_STATUS'],$row['COMVARS'],$row['CMD_DEFAULT_VALUES'],$row['COMSTRING'],$row['VARCOM'],$row['COMCOND'],$row['COMEFF'],$row['MAX_DURATION']);
				$iacommand->id =  $row['COMNAME'];
				$dbh->commit();
				Iacommand::cacheObject($iacommand);
				return $iacommand;
			}
			else {
				$dbh->commit();
				return null;
			}
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récupération de l'objet ayant l'id ". $COMNAME . " a échouée",0,$e);
		}
	}

	protected function update(){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$sql='UPDATE `iacommand` SET `COMCOM` = :COMCOM, `DEVICE_NAME` = :DEVICE_NAME, `COM_OPE` = :COM_OPE, `LOCKING_AUTHORIZATION` = :LOCKING_AUTHORIZATION, `LOCKING_STATUS` = :LOCKING_STATUS, `COMVARS` = :COMVARS, `CMD_DEFAULT_VALUES` = :CMD_DEFAULT_VALUES, `COMSTRING` = :COMSTRING, `VARCOM` = :VARCOM, `COMCOND` = :COMCOND, `COMEFF` = :COMEFF, `MAX_DURATION` = :MAX_DURATION WHERE `COMNAME` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':COMCOM'=>$this->COMCOM, ':DEVICE_NAME'=>$this->DEVICE_NAME, ':COM_OPE'=>$this->COM_OPE, ':LOCKING_AUTHORIZATION'=>$this->LOCKING_AUTHORIZATION, ':LOCKING_STATUS'=>$this->LOCKING_STATUS, ':COMVARS'=>$this->COMVARS, ':CMD_DEFAULT_VALUES'=>$this->CMD_DEFAULT_VALUES, ':COMSTRING'=>$this->COMSTRING, ':VARCOM'=>$this->VARCOM, ':COMCOND'=>$this->COMCOND, ':COMEFF'=>$this->COMEFF, ':MAX_DURATION'=>$this->MAX_DURATION, ':id'=>$this->id));
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La modification de l'objet de type ".get_class($this)." ayant l'id ". $this->id . " a échouée",0,$e);
		}
	}

	public function delete(){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$sql = 'DELETE FROM `iacommand` WHERE `COMNAME` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':id'=>$this->id));
			Iacommand::removeObjectFromCache($this->id);
			$this->id = null;
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La suppression de l'objet de type ".get_class($this)." ayant l'id ". $this->id. " a échouée",0,$e);
		}
	}

	protected function insert(){
		$dbh = SingletonPDO::getInstance();
		try{
			if (is_null($this->id)) {
				$dbh->beginTransaction();
				$sql='INSERT INTO `iacommand` ( `COMCOM`, `DEVICE_NAME`, `COM_OPE`, `LOCKING_AUTHORIZATION`, `LOCKING_STATUS`, `COMVARS`, `CMD_DEFAULT_VALUES`, `COMSTRING`, `VARCOM`, `COMCOND`, `COMEFF`, `MAX_DURATION`) VALUES (:COMCOM, :DEVICE_NAME, :COM_OPE, :LOCKING_AUTHORIZATION, :LOCKING_STATUS, :COMVARS, :CMD_DEFAULT_VALUES, :COMSTRING, :VARCOM, :COMCOND, :COMEFF, :MAX_DURATION)';
				$stmt = $dbh->prepare($sql);
				$stmt->execute(array(':COMCOM'=>$this->COMCOM, ':DEVICE_NAME'=>$this->DEVICE_NAME, ':COM_OPE'=>$this->COM_OPE, ':LOCKING_AUTHORIZATION'=>$this->LOCKING_AUTHORIZATION, ':LOCKING_STATUS'=>$this->LOCKING_STATUS, ':COMVARS'=>$this->COMVARS, ':CMD_DEFAULT_VALUES'=>$this->CMD_DEFAULT_VALUES, ':COMSTRING'=>$this->COMSTRING, ':VARCOM'=>$this->VARCOM, ':COMCOND'=>$this->COMCOND, ':COMEFF'=>$this->COMEFF, ':MAX_DURATION'=>$this->MAX_DURATION));
				$this->id = $dbh->lastInsertId();
				$dbh->commit();
			}
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("L'insertion de l'objet de type ".get_class($this)." a échouée",0,$e);
		}
	}

	public static function getAll( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql = 'SELECT * FROM `iacommand`';
			$sql .= self::generateWhereClause($search_params);
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$stmt->execute($array);
			while ($row = $stmt->fetch()) {
				$obj = null;
				if(Iacommand::existsInCache($row['COMNAME'])){
					$obj = Iacommand::getFromCache($row['COMNAME']);
				}
				else{
					$obj = new Iacommand( $row['COMNAME'],$row['COMCOM'],$row['DEVICE_NAME'],$row['COM_OPE'],$row['LOCKING_AUTHORIZATION'],$row['LOCKING_STATUS'],$row['COMVARS'],$row['CMD_DEFAULT_VALUES'],$row['COMSTRING'],$row['VARCOM'],$row['COMCOND'],$row['COMEFF'],$row['MAX_DURATION']);
				}
				if(!is_null($obj)){
					$obj->id = $row['COMNAME'];
					Iacommand::cacheObject($obj);
					$objs[] = $obj;
				}
			}
			$dbh->commit();
			return $objs;
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récuperation de la liste d'objets de type Iacommand a échouée",0,$e);
		}
	}

	public static function getCount( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql = 'SELECT count(*) as count FROM `iacommand`';
			$sql .= self::generateWhereClause($search_params);
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$stmt->execute($array);
			$count = $stmt->fetch();
			$dbh->commit();
			return $count['count'];
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récuperation de la liste d'objets de type Iacommand a échouée",0,$e);
		}
	}

	public static function deleteAll( $search_params = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql_delete = 'DELETE FROM `iacommand`';
			$sql_select = 'SELECT COMNAME FROM `iacommand`';
			$sql_delete .= self::generateWhereClause($search_params);
			$sql_select .= self::generateWhereClause($search_params);
			$stmt_delete = $dbh->prepare($sql_delete);
			$stmt_select = $dbh->prepare($sql_select);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$var_select = $stmt_select->execute($array);
			$tab_to_delete_objects = $stmt_select->fetchAll();
			if(sizeof($tab_to_delete_objects) > 0){
				$var_delete = $stmt_delete->execute($array);
				foreach($tab_to_delete_objects as $ind => $row){
					$existing_object = Iacommand::getObjectFromCache($row['COMNAME']);
					if(!is_null($existing_object)){
						$existing_object->id = null;
						Iacommand::removeObjectFromCache($row['COMNAME']);
					}
				}
			}
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La suppression de la liste d'objets de type Iacommand a échouée",0,$e);
		}
	}

}